import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

import '../../../widget/custom_cache_image_widget/index.dart';
import '../../../widget/price_Widget/index.dart';
import '../models/shopping_cart_model.dart';

class ShoppingCartGuess extends ConsumerStatefulWidget {
  final AsyncValue guessYouLike;

  const ShoppingCartGuess({super.key, required this.guessYouLike});

  @override
  _ShoppingCartGuessState createState() => _ShoppingCartGuessState();
}

class _ShoppingCartGuessState extends ConsumerState<ShoppingCartGuess> {
  @override
  Widget build(BuildContext context) {
    double imgWidth = (MediaQuery.of(context).size.width - 68) / 2;

    return ListView(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 20),
          child: Image.asset(
            'assets/user_like.png',
          ),
        ),
        widget.guessYouLike.when(
          data: (data) {
            return AlignedGridView.count(
              crossAxisCount: 2,
              mainAxisSpacing: 8,
              crossAxisSpacing: 8,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemCount: data.result.length,
              itemBuilder: (context, index) {
                GetGuessYouLikeResult getGuessYouLikeResult = data.result[index];
                return Container(
                  padding: const EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      GestureDetector(
                        onTap: () {
                          Beamer.of(context).beamToNamed('/goodsDetails/${getGuessYouLikeResult.id}');
                        },
                        child: SizedBox(
                          width: imgWidth,
                          height: imgWidth,
                          child: CustomCacheImageWidget(
                            imageUrl: getGuessYouLikeResult.picture,
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 6,
                      ),
                      Text(getGuessYouLikeResult.name),
                      const SizedBox(
                        height: 6,
                      ),
                      PriceWidget(price: getGuessYouLikeResult.price),
                    ],
                  ),
                );
              },
            );
          },
          error: (error, StackTrace) {
            return const Column(
              children: [Icon(Icons.error), Text("数据错误")],
            );
          },
          loading: () {
            return const Column(
              children: [SizedBox(height: 20), CircularProgressIndicator(), SizedBox(height: 20), Text("数据加载中")],
            );
          },
        ),
      ],
    );
  }
}
